package com.lh.java.basic.threadlocal;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestMultiplayThread {
	public static void main(String[] args) {
		GetSetName n = new GetSetName();
		ExecutorService pool = Executors.newFixedThreadPool(5);
		for(int i=0;i<20;i++){
			pool.submit(new MyThread(n));
		}
		pool.shutdown();
	}
}

class GetSetName {
	private static ThreadLocal<String> names = new ThreadLocal<String>(); 
	//private String name;

	public String getName() {
		return names.get();
	}

	public void setName(String name) {
		names.set(name);
	}
	
	public void del() {
		names.remove();
	}

}

class MyThread extends Thread {
   
	private GetSetName n;

	public MyThread(GetSetName n) {
		this.n = n;
	}
	

	@Override
	public void run() {
		n.setName("GetSetName+name" + Thread.currentThread().getId());
//		n.del();
		Random r = new Random();
		long sleeptime = r.nextInt(1000);
		try {
			Thread.sleep(sleeptime);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("thread =" + Thread.currentThread().getId()
				+ " thredname=" + Thread.currentThread().getName() +" sleeptime="+sleeptime+ " print "
				+ this.n.getName());
	}
}